function TreeNode(val) {
    this.val = val;
    this.left = null;
    this.right = null;
}

var zigzagLevelOrder = function (root) {
    if (!root) return [];
    let result = [];
    let node = [];
    let node_value = [];
    node.push(root)
    let n = 1;
    let x = 0;



    while (node.length) {

        node_value.push(node[0].val);

        if (node[0].left) {
            node.push(node[0].left);
        }
        if (node[0].right) {
            node.push(node[0].right);
        }

        if (node[0] === end_root) {
            result.push(node_value);
            node_value = [];
            end_root = node[node.length - 1];
            x = 1;
            n++;
        }

        node.shift();

        //[1,2,3,4,null,null,5] 5卡在这里需要翻转组数
        if (x && n % 2 == 0) {
            node.reverse();
            end_root = node[node.length - 1];
            x = 0;
        }

    }
    return result;
};
//root=4
//x=1
//n=3
//node=[5,4]
//node_value=[3,2]
//result=[[1],[3,2],]


// 构建示例二叉树
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
// root.right.left = new TreeNode(15);
root.right.right = new TreeNode(5);
//        1
//      2   3
//    4       5
//


console.log(zigzagLevelOrder(root));



